Data Visualization¶

Matplotlib¶

Graph 1: Histogram¶

Histogram to visualize the distribution of a variable.

See Matplotlib Hist

In [1]:
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('_mpl-gallery')

# make data
np.random.seed(1)
x = 4 + np.random.normal(0, 1.5, 200)

# plot:
fig, ax = plt.subplots()

ax.hist(x, bins=8, linewidth=0.5, edgecolor="white")

ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
       ylim=(0, 56), yticks=np.linspace(0, 56, 9))

plt.show()

Seaborn¶

Graph 2: Nested Boxplot¶

Box plot to show the distribution of a variable across different categories.

See Nested Boxplot

In [2]:
import seaborn as sns
sns.set_theme(style="ticks", palette="pastel")

# Load the example tips dataset
tips = sns.load_dataset("tips")

# Draw a nested boxplot to show bills by day and time
sns.boxplot(x="day", y="total_bill",
            hue="smoker", palette=["m", "g"],
            data=tips)
sns.despine(offset=10, trim=True)

Plotly¶

Graph 3: Density heatmaps (2D Histograms)¶

Density heatmap to visualize the relationship between total bill and tip, considering factors like sex and smoking status.

See Density heatmaps with Plotly Express

In [3]:
import plotly.express as px
import plotly

plotly.offline.init_notebook_mode()

df = px.data.tips()

fig = px.density_heatmap(df, x="total_bill", y="tip", facet_row="sex", facet_col="smoker")
fig.show()

Data Visualization Comparison¶

Package Plot Type Description
Matplotlib Histogram Distribution of a variable ~matplotlib.axes.Axes
Seaborn Nested Boxplot Distribution of total bills by day and time, differentiated for smokers and non-smokers boxplot
Plotly Density Heatmap Relationship between total bill and tip, considering factors like sex and smoking status density_heatmaps